<?php
namespace App\Http\Controllers\Api\Miniprogram;

use App\Http\Controllers\Controller;
use App\Models\User\User;
use App\Models\Wechat\WechatMiniUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function session()
    {
        $code = request('code');
        $microApp = resolve('microApp', ['aliasname'=>'miniprogram']);
        $wechat_app = resolve('wechatApp', [
            'aliasname'=>'miniprogram',
        ]);
        $utils = $wechat_app->getUtils();
        $session = $utils->codeToSession($code);
//        $session['openid'] = 'og5wT7Qtbo3HXGbLMPCYs2Jm3U6g';
        $miniUser = WechatMiniUser::updateOrCreate([
            'openid'=>$session['openid'],
        ], [
            'sys_account_id'=>$microApp->sys_account_id,
            'unionid'=>@$session['unionid'] ?? null,
        ]);
        if ($miniUser->user_id == 0) {
            $user = User::create([]);
            $miniUser->user_id = $user->id;
            $miniUser->save();
        } else {
            $user = $miniUser->user;
        }
        Auth::login($user);
        $user = Auth::user();
//        $user->tokens()->delete();
        if (!$user->token) {
            $token = $user->createToken('api_token')->plainTextToken;
            $user->token = $token;
            $user->save();
        } else {
            $token = $user->token;
        }
        return $this->successJson([
            'token'=>$token,
            'session'=>$session,
            'user'=>$miniUser->user,
        ]);
    }

    public function userinfo()
    {
        $user = Auth::user();
        $user = User::with(['avatarFile', 'wechatMiniUser'])->where('id', $user->id)->first();
        return $this->successJson($user);
    }

    public function getPhone()
    {
        $data = [
            'code' => (string) request()->get('code'),
        ];
        $wechat_app = resolve('wechatApp', [
            'aliasname'=>'miniprogram',
        ]);

        $phoneInfo = $wechat_app->getClient()->postJson('wxa/business/getuserphonenumber', $data);
        if ($phoneInfo['errcode'] == 0) {
            $phone = $phoneInfo['phone_info']['phoneNumber'];
            $user = Auth::user();
            $user->phone = $phone;
            $user->status = 1;
            $user->save();
            return $this->successJson($user);
        } else {
            return $this->failJson('user.unable_get_phone_number');
        }
    }

    public function saveUserInfo(Request $request)
    {
        $user = Auth::user();
        $name = $request->post('name');
        $user->name = $name;
        $avatar = $request->post('avatar');
        if ($avatar) {
            $user->avatar = $avatar;
        }
        $phone = $request->post('phone');
        if ($phone) {
            $user->phone = $phone;
        }
        $address = $request->post('address');
        if ($address) {
            $user->address = $address;
        }
        $sex = $request->post('sex');
        if ($sex) {
            $user->sex = $sex;
        }
        $user->save();
        return $this->successJson();
    }
}
